package net.lahuobao.mall.controller.admin;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.lahuobao.mall.entity.admin.*;
import net.lahuobao.mall.service.admin.*;
import org.apache.commons.httpclient.HttpException;
import org.apache.log4j.Logger;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.jeecgframework.core.beanvalidator.BeanValidators;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.net.URI;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;

/**   
 * @Title: Controller
 * @Description: ETC卡申请管理
 * @author zhangdaihao
 * @date 2016-12-02 16:48:01
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/mallEtcApplyController")
public class MallEtcApplyController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MallEtcApplyController.class);

	@Autowired
	private MallEtcApplyServiceI mallEtcApplyService;

	@Autowired
	private SystemService systemService;

	@Autowired
	private Validator validator;

	@Autowired
	private MallEtcCardRuleServiceI cardRuleService;

	@Autowired
	private MallExpresscompanyServiceI expresscompanyService;

	@Autowired
	private MallConsigneeaddServiceI consigneeaddService;

	@Autowired
	private MallRegionServiceI regionService;

	/**
	 * ETC卡申请管理列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("net/lahuobao/mall/admin/mallEtcApplyList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(MallEtcApplyEntity mallEtcApply,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(MallEtcApplyEntity.class, dataGrid);

		//申请单号
		if(StringUtil.isNotEmpty(mallEtcApply.getApplyno())){
			cq.like("applyno", "%"+mallEtcApply.getApplyno()+"%");
			mallEtcApply.setApplyno(null);
		}

		//车牌号
		if(StringUtil.isNotEmpty(mallEtcApply.getCarno())){
			cq.like("carno", "%"+mallEtcApply.getCarno()+"%");
			mallEtcApply.setCarno(null);
		}

		//店铺
		cq.createAlias("shop","MallShopEntity");
		/*店铺名称*/
		if(null != mallEtcApply.getShop() && StringUtil.isNotEmpty(mallEtcApply.getShop().getShopname())){
			cq.like("MallShopEntity.shopname", "%"+mallEtcApply.getShop().getShopname()+"%");
			mallEtcApply.getShop().setShopname(null);
		}
		//状态大于0
		cq.gt("state", 0);

		//排序
		cq.addOrder("applytime", SortDirection.desc);
		//查询条件组装器

		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mallEtcApply, request.getParameterMap());
		this.mallEtcApplyService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除ETC卡申请管理
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(MallEtcApplyEntity mallEtcApply, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		mallEtcApply = systemService.getEntity(MallEtcApplyEntity.class, mallEtcApply.getId());
		message = "ETC卡申请管理删除成功";
		mallEtcApplyService.delete(mallEtcApply);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		j.setMsg(message);
		return j;
	}

	/**
	 * 获取分配Etc卡号页面
	 */
	@RequestMapping(params = "configetcview")
	public ModelAndView mallConfigEtcCradNo(Integer id) {
		ModelAndView mv = new ModelAndView("net/lahuobao/mall/admin/mallConfigEtcCardNo");
		MallEtcApplyEntity etcApply = mallEtcApplyService.get(MallEtcApplyEntity.class, id);
		mv.addObject("id", id);
		mv.addObject("shopid", etcApply.getShopid());
		mv.addObject("receivetype", etcApply.getReceivetype());
		mv.addObject("orderId", etcApply.getOrderid());
		//领卡方式为邮寄--获取快递公司
		if(etcApply.getReceivetype() == 1){
			mv.addObject("expresscompanys", expresscompanyService.loadAll(MallExpresscompanyEntity.class));
		}

		return mv;
	}


	/**
	 * 分配卡号
	 */
	@RequestMapping(params = "config")
	@ResponseBody
	public AjaxJson configEtcCradNO(MallConfigEtcEntity mallConfigEtc) {
		AjaxJson json = new AjaxJson();
		try {
			mallEtcApplyService.configEtcCardNo(mallConfigEtc);
		}catch (HttpException e){
			json.setSuccess(false);
			json.setMsg("系统异常!");
			e.printStackTrace();
		}catch (IOException e){
			json.setSuccess(false);
			json.setMsg("系统异常!");
			e.printStackTrace();
		}catch (Exception e) {
			json.setSuccess(false);
			json.setMsg(e.getMessage());
			e.printStackTrace();
		} finally {
			return json;
		}

	}

	/**
	 * 获取该商品陪分配的ETC卡号
	 * @param mallEtcCardRule
	 * @param request
	 * @param response
	 * @param dataGrid
	 */
	@RequestMapping(params = "etccardatagrid")
	public void datagrid(MallEtcCardRuleEntity mallEtcCardRule,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(MallEtcCardRuleEntity.class, dataGrid);
		//卡号
		if(StringUtil.isNotEmpty(mallEtcCardRule.getCardno())){
			cq.like("cardno","%"+ mallEtcCardRule.getCardno()+"%");
			mallEtcCardRule.setCardno(null);
		}
		//状态--未使用
		cq.eq("state", 0);
		//店铺
		cq.eq("shopid", mallEtcCardRule.getShopid());
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mallEtcCardRule, request.getParameterMap());
		cardRuleService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}


	/**
	 * ETC卡申请管理列表页面跳转
	 *
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(MallEtcApplyEntity mallEtcApply, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(mallEtcApply.getId())) {
			mallEtcApply = mallEtcApplyService.getEntity(MallEtcApplyEntity.class, mallEtcApply.getId());
			//领卡方式为邮寄显示领卡方式
			if(1 == mallEtcApply.getReceivetype()){
				MallConsigneeaddEntity consigneeaddEntity = consigneeaddService.getEntity(MallConsigneeaddEntity.class, mallEtcApply.getMailingAddressId());
				String[] str = regionService.getAddressByCode(Integer.parseInt(consigneeaddEntity.getAreacode())).split(";");
				String reStr = "";
				for (int i = 0; i < str.length; i++) {
					reStr += str[i].split("_")[0];
				}
				consigneeaddEntity.setAddressName(reStr);
				req.setAttribute("mailingAddress", consigneeaddEntity);
			}
			req.setAttribute("mallEtcApplyPage", mallEtcApply);
		}
		return new ModelAndView("net/lahuobao/mall/admin/mallEtcApply");
	}
}
